from rest_framework.fields import SerializerMethodField
from rest_framework.serializers import ModelSerializer

from books.models import BooksModel, TypeModel, WriterModel, CreateAddressModel, CommitModel


#图书分类序列化器
class TypeSer(ModelSerializer):
    class Meta:
        model = TypeModel
        fields = '__all__'

#作者表序列化器
class WriterSer(ModelSerializer):
    class Meta:
        model = WriterModel
        fields = '__all__'

#出版社序列化器
class CreAddressSer(ModelSerializer):
    class Meta:
        model = CreateAddressModel
        fields = '__all__'

#图书序列化器
class BooksSer(ModelSerializer):
    tname=SerializerMethodField()
    def get_tname(self,obj):
        return obj.typename.typename
    wname = SerializerMethodField()

    def get_wname(self, obj):
        return obj.writername.writername
    crename = SerializerMethodField()

    def get_crename(self, obj):
        return obj.createname.crestename
    isdianzi_display = SerializerMethodField(read_only=True)

    def get_isdianzi_display(self, obj):
        return obj.get_isdianzi_display()
    bookslevel_display = SerializerMethodField(read_only=True)

    def get_bookslevel_display(self, obj):
        return obj.get_bookslevel_display()
    isbuy_display = SerializerMethodField(read_only=True)

    def get_isbuy_display(self, obj):
        return obj.get_isbuy_display()
    class Meta:
        model=BooksModel
        fields='__all__'

#评价序列化器
class CommitSer(ModelSerializer):
    class Meta:
        model=CommitModel
        fields='__all__'

